/**
 * Created by liuqingxia on 2016/3/10.
 */

var removeDataSourceId;//要删除的DataSource的id

var dataSourceTable;


var params = {
    pageSize: 10
};

$(document).ready(function () {

    LTS.colFormatter.optFormat = function (v, row) {
        return generateOptButtons(row);
    }

    LTS.colFormatter.emptyString = function (v, row) {
        return emptyString(v);
    }

    LTS.colFormatter.nameFormat = function (v, row) {
        return generateSourceName(row);
    }

    LTS.colFormatter.typeFormat = function (v, row) {
        return getTypeStr(row);
    }

    dataSourceTable = $("#datasource-table").ltsTable({
        url: getUrlBySuffix("/datasource/find"),
        templateId: 'datasource-table-template'
    });

    dataSourceTable.post(params, 1);

    clickRemoveBtn();
    clickConfirmRemoveBtn();
    clickUpdateBtn();
    clickConfirmUpdateBtn();
});

/**
 * 点击删除按钮
 */
function clickRemoveBtn() {
    $(document).on("click", ".remove-btn", function () {
        removeDataSourceId = getCurrentId($(this));
        $("#remove-tip-modal").modal("show");
    });
}

/**
 * 点击确认删除按钮
 */
function clickConfirmRemoveBtn() {
    $(document).on("click", "#confirm-remove-btn", function () {
        $("#cancel-remove-btn").trigger("click");//提示框隐藏
        var params = {};
        params['id'] = $.trim(removeDataSourceId);
        $.postHttp(params, getUrlBySuffix("/datasource/remove"), function(json){
        	if (json && json.success) {
                swal({
    				title : "操作成功",
    				text : json['message'],
    				type : "success"
    			}, function() {
    				 var param = {pageSize: 10};
    	             dataSourceTable.post(param, pageNum);
    	             $("#dataSource-edit-modal").modal("hide");
    			});
            } else {
                if (json) {
                    swal("失败", json['message'], 'error');
                }
            }
        });
    });
}

/**
 * 点击编辑按钮
 */
function clickUpdateBtn() {
    $(document).on("click", ".update-btn", function () {
        var id = getCurrentId($(this));
        var dataSource = getObjectById(getUrlBySuffix("/datasource/findById"), id);
        fillDataSourceEditForm(dataSource);//将dataSource对象各属性值赋值到任务编辑表单
        $("#dataSource-edit-modal").modal("show");//显示任务编辑模态框
    });
}

/**
 * 点击任务编辑模态框上的修改按钮
 */
function clickConfirmUpdateBtn() {
    $(document).on("click", "#edit-btn", function () {
    	$("#dataSource-edit-modal").modal("hide");
        var params = getFormParams("editForm");
        $.postHttp(params, getUrlBySuffix("/datasource/update"), function(json){
        	if (json && json.success) {
                swal({
    				title : "操作成功",
    				text : json['message'],
    				type : "success"
    			}, function() {
    				 var param = {pageSize: 10};
    	             dataSourceTable.post(param, pageNum);
    	             $("#dataSource-edit-modal").modal("hide");
    			});
            } else {
                if (json) {
                    swal("失败", json['message'], 'error');
                }
            }
        });
        //$("#job-edit-modal-dismiss").modal("hide");
        //$("#searchBtn").trigger("click");
    });
}

/**
 * 生成数据源表达式
 * @param dataSource
 * @returns {string}
 */
function generateSourceName(dataSource) {
    var expression = dataSourceName(dataSource['userName'], dataSource['jdbcUrl']);

    return expression;
}

/**
 * 获取数据源类型
 * @param dataSource
 * @returns {string|*}
 */
function getTypeStr(dataSource) {
    var typeInt = dataSource['sourceType'];
    return switchTypeInt(typeInt);
}

/**
 * 根据int值，返回对应的数据源类型
 * @param typeInt
 * @returns {string|*}
 */
function switchTypeInt(typeInt) {
    typeInt = $.trim(typeInt);
    var sourceType;
    switch (typeInt) {
        case "1":
            sourceType = "mysql";
            break;
        case "2":
            sourceType = "drds";
            break;
        case "3":
            sourceType = "oracle";
            break;
        case "4":
            sourceType = "sqlserver";
            break;
    }
    return sourceType;
}

/**
 * 把对象的各属性赋值到编辑表格中
 * @param obj
 */
function fillDataSourceEditForm(obj) {
    $.each($('#editForm').parent().find(".form-control"), function () {
        var name = $(this).attr("name");
        if (isNull(name)) {
            return true;
        }
        name = $.trim(name);
        var value = obj[name];
        value = $.trim(value);
        $(this).val(value);
    });
}